package com.h3c.ptability.mapper;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Constants;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.h3c.ptability.dto.PtUserDeptMonthDTO;
import com.h3c.ptability.entity.PtUserDeptMonth;
import com.h3c.ptability.vo.PtUserDeptMonthVO;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.List;
import java.util.Map;

/**
 * PtUserDeptMonthMapper
 *
 * @author h24002
 * @date 2022-10-27
 */
@Mapper
public interface PtUserDeptMonthMapper extends BaseMapper<PtUserDeptMonth> {

    /**
     * 获取用户授权部门数据--分页型
     * @param userDeptMonthQueryWrapper 查询条件
     * @param page 分页模型
     * @return 用户授权部门数据（含部门等级，部门名称，员工名称等）
     */
    @Select("SELECT pudm.*,pd.parent_id,pd.dept_name,pd.dept_name_en,pd.dept_level,CONCAT(paw.worker_id,' ',paw.worker_name) AS user_name " +
            "FROM pt_user_dept_month pudm " +
            "LEFT JOIN pt_dept pd ON pudm.dept_id = pd.dept_id and pd.dept_status=1 " +
            "LEFT JOIN pt_all_worker paw ON paw.worker_id=pudm.user_id " +
            "${ew.customSqlSegment}")
    IPage<PtUserDeptMonthVO> selectPtUserDeptMonthInPage(@Param(Constants.WRAPPER) QueryWrapper<PtUserDeptMonth> userDeptMonthQueryWrapper, Page<PtUserDeptMonth> page);

    /**
     * 获取用户授权部门数据
     * @param userDeptMonthQueryWrapper 查询条件
     * @return 用户授权部门数据（含部门等级，部门名称，员工名称等）
     */
    @Select("SELECT pudm.*,pd.parent_id,pd.dept_name,pd.dept_name_en,pd.dept_level,CONCAT(paw.worker_id,' ',paw.worker_name) AS user_name " +
            "FROM pt_user_dept_month pudm " +
            "LEFT JOIN pt_dept pd ON pudm.dept_id = pd.dept_id and pd.dept_status=1 " +
            "LEFT JOIN pt_all_worker paw ON paw.worker_id=pudm.user_id " +
            "${ew.customSqlSegment}")
    List<PtUserDeptMonthVO> selectPtUserDeptMonth(@Param(Constants.WRAPPER) QueryWrapper<PtUserDeptMonth> userDeptMonthQueryWrapper);

    /**
     * 删除无效部门数据
     * @param appId 应用编码
     * @param dateId 操作日期
     * @return 删除数量
     */
    @Update("DELETE pudm FROM pt_user_dept_month pudm " +
            "LEFT JOIN pt_dept pd ON pudm.dept_id = pd.dept_id and pd.dept_status=1 " +
            "WHERE pudm.app_id=#{appId} AND pudm.date_id=#{dateId} AND pd.dept_id IS NULL ")
    Integer deletePtUserDeptMonth(@Param("appId") String appId, @Param("dateId") Integer dateId);

    /**
     * 初始化部门数据
     * @param appId 应用编码
     * @param dateId 操作日期
     * @param operatorId 操作人工号
     * @param preDateId 上月度日期
     * @return 删除数量
     */
    @Update("INSERT INTO pt_user_dept_month(app_id,dept_id,date_id,user_id,operate_time,operator_id) " +
            "SELECT app_id,dept_id,#{dateId} AS date_id,user_id,CURDATE() AS operate_time,#{operatorId} AS operator_id " +
            "FROM pt_user_dept_month WHERE app_id=#{appId} AND date_id=#{preDateId} ")
    Integer InitPtUserDeptMonth(@Param("appId") String appId, @Param("dateId") Integer dateId, @Param("operatorId") String operatorId, @Param("preDateId") Integer preDateId);



    List<PtUserDeptMonthVO> selectChildDeptMonth(PtUserDeptMonthDTO dto);
}
